<?php

namespace App\Models;

use Illuminate\Database\Eloquent\Model;

class Game extends Model
{
    public static $rules = [
        'name' => 'required',
        'size' => 'required',
        'icon' => 'required',
        'download_url' => 'required'
    ];

    protected $guarded = ['id'];

    protected $casts = [
        'id' => 'int',
        'download_counts' => 'int',
        'top' => 'boolean',
        'banner' => 'boolean',
    ];

    protected static function boot()
    {
        static::bootTraits();
        static::created(function($item){
            if($category = $item->category){
                $category->increment('games_count');
            }
        });
    }


    public function getDownloadUrlAttribute($value)
    {
        return url('games/download', $this->id);
    }

    public function category(){
        return $this->belongsTo('App\Models\Category', 'category_slug', 'slug');
    }
}

